package com.bdh.javaBasicStudy;

import com.sun.xml.internal.messaging.saaj.packaging.mime.util.QEncoderStream;
import org.junit.Test;

import java.util.*;

/**
 * @projectName: maven_project
 * @package: com.bdh.javaBasicStudy
 * @className: ListMapSetExer
 * @author: Dream
 * @description: 区别及其遍历方式
 * Collection:单列集合，用来存储一个一个的对象
 *      List:存储有序的，可重复的数据 ---》"动态"数组
 *              ArrayList\LinkedList\Vector
 *                  ArrayList\LinkedList 线程不安全 LinkedList用双向链表 ArrayList用Object[] elementData
 *                      LinkedList:Deque\List\Queue\Cloneable\Serializable
 *      Set:存储无序的、不可重复的数据 ---》集合
 *              HashSet\LinkedList\TreeSet
 * Map:双列集合、用来存储一对（key-value）的数据 ---》函数
 *      HashMap\LinkedHashMap\TreeMap\Hashtable\Properties
 * @date: 2022/6/17 08:03
 */
public class ListMapSetExer {
    @Test
    public void arrayListExer(){
        ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
        arrayList1.add(1);
        arrayList1.add(2);
        arrayList1.add(3);
        arrayList1.add(4);
        //增强for循环输出
        for (int i :
                arrayList1) {
            System.out.print(i);
        }
        //迭代器输出
        Iterator<Integer> iterator = arrayList1.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next());
        }
    }
    //用栈实现队列
    Stack<Integer> stackIn;
    Stack<Integer> stackOut;
    @Test
    public void linkedListExer1(){
        //用栈实现队列
        stackIn = new Stack<Integer>();
        stackOut = new Stack<Integer>();

    }

    public void push(int x) {
        stackIn.push(x);
    }

    public void dumpStackIn(){
        if (stackOut.isEmpty()){
            while(!stackIn.isEmpty()){
                stackOut.push(stackIn.pop());
            }
        }else {
            return;
        }
    }

    public int pop() {
        dumpStackIn();
        return stackOut.pop();
    }

    public int peek() {
        dumpStackIn();
        return stackOut.peek();
    }

    public boolean empty() {
        if(stackIn.isEmpty() && stackOut.isEmpty()) return true;
        else return  false;
    }


    //用队列实现栈
    Queue<Integer> queue;
    @Test
    public void linkedListExer2(){
        queue = new LinkedList<Integer>();
    }
    public void pushQueue(int x) {
        queue.offer(x);
    }

    public int popQueue() {
        while(queue.size() != 1){
            queue.offer(queue.poll());
        }
        return queue.poll();
    }

    public int topQueue() {
        while(queue.size() != 1){
            queue.offer(queue.poll());
        }
        int top = queue.peek();
        queue.offer(queue.poll());
        return top;
    }

    public boolean emptyQueue() {
        if(queue.isEmpty()) return true;
        else return false;
    }

    //判断括号是否有效
    @Test
    public void testVaild(){
        String s = "([{()}]}";
        if(isValid(s)) System.out.println("t");
        else System.out.println("f");
    }
    public boolean isValid(String s){
        Stack<Character> stack = new Stack<Character>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '{' || s.charAt(i) == '[' || s.charAt(i) == '('){
                stack.push(s.charAt(i));
            }else {
                if(stack.isEmpty()) return false;
                char top = stack.pop();
                char temp = s.charAt(i);
                if(temp == ')' && top !='('){
                    return false;
                }else if(temp == '}' && top != '{'){
                    return false;
                }else if (temp == ']' && top != '['){
                    return false;
                }
            }
        }
        if(stack.isEmpty()){
            return true;
        }else{
            return false;
        }
    }
}
